const app = new getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    prompt: '',
    default_img: '',
    user_info: {},
    file_Path: '',
    saveHeadImgFlag: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    app.defaultImg(that, true);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  loadInfo: function() {
    let that = this;
    app.sendRequest({
      url: 'System.Member.memberInfo',
      success: function(res) {
        if (res.code == 0) {
          let user_info = app.checkEmpty(res.data.user_info, {});
          user_info.user_headimg = app.checkEmpty(user_info.user_headimg, '');
          user_info.user_headimg = app.IMG(user_info.user_headimg); //图片路径处理
          that.setData({
            user_info: user_info
          })
        }
      }
    })
  },

  /**
   * 头像加载失败
   */
  errorHeadImg: function(e) {
    let that = this;
    let user_info = that.data.user_info;
    let default_img = that.data.default_img;
    let img = user_info.user_headimg;
    let parm = {};
    let parm_key = "user_info.user_headimg";

    if (default_img.is_use == 1) {
      default_img = default_img.value.default_headimg;
      if (img.indexOf(default_img) == -1) {
        parm[parm_key] = default_img;
        that.setData(parm);
      }
    }
  },

  /**
   * 上传图片至微信服务器
   */
  uploadImg: function() {
    let that = this;

    let file_Path = that.data.file_Path;
    let tempFiles = that.data.tempFiles;
    //选择图片
    wx.chooseImage({
      count: 1,
      sizeType: 'compressed',
      success: function(res) {
        let file_Path = res.tempFilePaths[0];
        let tempFiles = res.tempFiles[0];
        that.setData({
          file_Path: file_Path
        })
      },
      fail: function(res) {
        app.showBox(that, '无法获取本地图片');
        console.log(res);
      }
    })
  },

  /**
   * 上传至服务器
   */
  uploadHeadImg: function() {
    let that = this;
    let file_Path = that.data.file_Path;
    let base = app.globalData.siteBaseUrl;
    let saveHeadImgFlag = that.data.saveHeadImgFlag;

    if (saveHeadImgFlag == 1) {
      return false;
    }
    app.clicked(that, 'saveHeadImgFlag');

    if (file_Path == '') {
      wx.navigateBack({
        delta: 1
      })
    }

    let param = {
      token: app.globalData.token,
      file_path: 'comment',
      method: 'System.Upload.uploadImage'
    };
    param.sign = app.encodeSign(param);
    param.private_key = app.globalData.private_key;

    wx.uploadFile({
      url: base + 'api.php?method=System.Upload.uploadImage',
      filePath: file_Path,
      name: 'img_upload',
      formData: param,
      success: function(res) {

        let data = res.data;

        try {
          data = JSON.parse(JSON.parse(data));
        } catch (e) {
          console.log(e);
          app.showBox(that, '图片上传失败');
        }

        let code = data.code;
        if (code == 0) {
          data = data.data;
          let code = data.code;
          let message = data.message;
          let img_url = data.data;

          if (code > 0) {
            that.saveHeadImg(that, img_url);
          } else {
            app.showBox(that, message);
            app.restStatus(that, 'saveHeadImgFlag');
          }

        } else {
          app.showBox(that, '保存失败');
          app.restStatus(that, 'saveHeadImgFlag');
        }
      },
      fail: function(res) {
        console.log(res);
        app.showBox(that, '保存失败');
        app.restStatus(that, 'saveHeadImgFlag');
      }
    })
  },

  /**
   * 保存头像
   */
  saveHeadImg: function(that, img_url) {

    app.sendRequest({
      url: 'System.Member.modifyFace',
      data: {
        user_headimg: img_url
      },
      success: function(res) {
        let code = res.code;
        let data = res.data;

        if (code == 0) {

          if (data > 0) {
            wx.navigateBack({
              delta: 1
            })
          } else {
            app.showBox(that, '保存失败');
            app.restStatus(that, 'saveHeadImgFlag');
          }
        }
      }
    });
  }
})